﻿//创建时间: 2011-05-30
//创建作者: 李 城
//功能说明: CMS设置
//最后修改: 2011-05-30
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using TRPOP.Common;
using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using System.Text;

namespace TRPOP.CMS.Web.ExtraBus
{
    /// <summary>
    /// 逻辑处理继承的基类
    /// </summary>
    public class Page_Set : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (RequestHelper.RequestString("type").Trim().ToLower() == "nodeset")
                {
                    SaveOneNode(RequestHelper.RequestInt("nodeid"));
                }
                else if (RequestHelper.RequestString("type").Trim().ToLower() == "upfile")
                {
                    SaveOneNodeUpFile(RequestHelper.RequestInt("nodeid"));
                }
                else
                    LoadAllNode();
            }
        }

        protected void SaveOneNode(int intNodeId)
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.ClearContent();
            NodeManage oNodeManage =  new NodeManage();
            NodeInfo oNodeInfo = oNodeManage.GetInfoById(intNodeId);
            if (oNodeInfo == null || oNodeInfo.NodeId<=0)
            {
                Response.Write("error");
                Response.End();
            }
            if (oNodeManage.Save(oNodeInfo) == EnumSubmitResult.Success)
            {
                Response.Write(oNodeInfo.Name);
                Response.End();
            }
            else
            {
                Response.Write("error");
                Response.End();
            }
        }

        protected void SaveOneNodeUpFile(int intNodeId)
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.ClearContent();
            NodeManage oNodeManage = new NodeManage();
            SerializableDictionary<string, string> oDic = new SerializableDictionary<string, string>();
            if (intNodeId == 0)
            {
                oDic = oNodeManage.SaveNullNodeURLForSourcePSN();
                if (oDic.ContainsKey("0_PSNSourceRoot") && oDic.ContainsKey("0_PSNSourceRootURL"))
                {
                    new ContentUpFileManage().UpdateFileURL(intNodeId, oDic["0_PSNSourceRoot"], oDic["0_PSNSourceRootURL"]);
                }
                Response.Write("默认结点");
                Response.End();
            }
            else if (intNodeId > 0)
            {
                NodeInfo oNodeInfo = oNodeManage.GetInfoById(intNodeId);
                if (oNodeInfo == null)
                {
                    Response.Write("error");
                    Response.End();
                }
                oDic = oNodeManage.SaveNodeURLForSourcePSN(oNodeManage.GetInfoById(intNodeId));
                if (oDic.ContainsKey(oNodeInfo.NodeId.ToString() + "_PSNSourceRoot") && oDic.ContainsKey(oNodeInfo.NodeId.ToString() + "_PSNSourceRootURL"))
                {
                    new ContentUpFileManage().UpdateFileURL(intNodeId, oDic[oNodeInfo.NodeId.ToString() + "_PSNSourceRoot"], oDic[oNodeInfo.NodeId.ToString() + "_PSNSourceRootURL"]);
                }
                Response.Write(oNodeInfo.Name);
                Response.End();
            }
            else
            {
                Response.Write("error");
                Response.End();
            }
        }

        protected string LoadAllNode()
        {
            StringBuilder strbuild = new StringBuilder();
            foreach (NodeInfo oTempNode in new NodeManage().GetListByCondition(0, "  and IsHidde=0  and ( ParentPath is null or ArrChildId is null )  ", "  DisplayOrder desc  ,nodeid asc "))
            {
                strbuild.Append(oTempNode.NodeId + ",");
            }
            if (strbuild.Length > 0)
            {
                strbuild.Remove(strbuild.Length - 1, 1);
                return  strbuild.ToString();
            }
            return "";
        }
        protected string LoadAllNode2()
        {
            StringBuilder strbuild = new StringBuilder("0,");
            foreach (NodeInfo oTempNode in new NodeManage().GetListByCondition(0, "  ", "  DisplayOrder desc  ,nodeid asc "))
            {
                strbuild.Append(oTempNode.NodeId + ",");
            }
            if (strbuild.Length > 0)
            {
                strbuild.Remove(strbuild.Length - 1, 1);
                return strbuild.ToString();
            }
            return "";
        }
    }
}